package com.dengcj.ecology.request;

import cn.hutool.db.Entity;
import com.dengcj.db.DB;
import com.dengcj.ecology.request.entity.NodeExecuterResult;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.List;
import java.util.Map;

/**
 * @author dengchunjie
 * @Description: 节点附加操作Action模板代码
 * @date 2022/3/18 13:39
 */
public class TemplateAction implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String workflowId = requestInfo.getWorkflowid();
        String tableName = requestInfo.getRequestManager().getBillTableName();
        NodeExecuterResult nodeExecuterResult = new NodeExecuterResult();

        // 优先选择从RequestInfo对象中拿主表数据，减轻数据库压力
        Entity mainFormData = NodeActionUtil.getMainFormData(requestInfo);
        int id = mainFormData.getInt("id");
        String code = mainFormData.getStr("code");
        double price = mainFormData.getDouble("price");

        // 从RequestInfo对象中中拿明细表数据，优先选择java程序处理，可以参考RequestDataProcessUtil处理简单的明细表需求
        Map<String, List<Entity>> detailFormData = NodeActionUtil.getDetailFormData(requestInfo);
        // 明细表1数据
        List<Entity> dt1 = detailFormData.get("dt1");

        // 需要本地调试的时候直接从数据拿主表数据，可模拟提交流程，调试完之后注释
        // ------------------- 本地调试用代码 START -------------------
//        DB db = new DB();
//        Entity mainFormData = db.queryOne("select * from "+tableName+" where requestid=?", requestid);
//        List<Entity> dt1 = db.query("select * from "+tableName+"dt1 where id=?", mainFormData.getInt("id"));
        // ------------------- 本地调试用代码 END -------------------

        if(price > 100){
            requestInfo.getRequestManager().setMessage("表单校验失败");
            requestInfo.getRequestManager().setMessagecontent("价格不能大于100");
        }

        return Action.SUCCESS;
    }
}
